//
//  LCVideoDetailChangeHeaderController.m
//  JKFuYinProject
//
//  Created by admin on 2018/7/18.
//  Copyright © 2018年 LC. All rights reserved.
//

#import "LCVideoDetailChangeHeaderController.h"
#import "MJRefresh.h"
#import "LCVideoHomeListAboutModel.h"
#import "UIViewController+LCMobShare.h"

#import "LTHeaderView.h"
#import "LCVideoAboutViewController.h"
#import "LCVideoDetailChangeHeaderController+LCPingLun.h"

#import "LCVideoWithListsViewController.h"
#import "LCVideoDetailsListsViewController.h"

@interface LCVideoDetailChangeHeaderController ()

@property(copy, nonatomic) NSArray <UIViewController *> *viewControllers;
@property(copy, nonatomic) NSArray <NSString *> *titles;
@property(strong, nonatomic) LTHeaderView *titleHeaderView;
@property(strong, nonatomic) LCVideoAboutViewController *videoAboutViewController;
@property(strong, nonatomic) LCVideoDetailsListsViewController *videoDetailsListsViewController;
@property(strong, nonatomic) UIView *sideslippingView;

@end

@implementation LCVideoDetailChangeHeaderController

- (void)dealloc {
    [[YYKeyboardManager defaultManager] removeObserver:self];
    @try {
        [self.tabPageController.tabBar removeObserver:self forKeyPath:@"selectedItemIndex"];
    } @catch (NSException *exception) {}

//    NSLog(@"NSHomeDirectory() = %@", NSHomeDirectory());
    NSLog(@"__func__dealloc = %s", __func__);
}

- (void)viewDidLoad {
    [super viewDidLoad];

    self.title = @"视频";
    self.view.backgroundColor = [UIColor whiteColor];
    self.automaticallyAdjustsScrollViewInsets = NO;
    [self initViewControllers];

    self.tabPageController.tabBar.itemTitleFont = [LSZSpecialPackaging setlabelTextFontAdaptiveSize:15];//[UIFont systemFontOfSize:15];
    self.tabPageController.tabBar.indicatorScrollFollowContent = YES;
    self.tabPageController.tabBar.indicatorColor = redUIColor;         // item指示器颜色
    self.tabPageController.tabBar.itemTitleSelectedColor = redUIColor;      // 选中时标题的颜色
    self.tabPageController.tabBar.itemTitleColor = RGBColor(140, 140, 140); // 标题的颜色
//    [self setupSubViews];
    [self onlineVideoData];
//
    [self createPingLun];
    [self setupSideslip];//侧滑
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"home_nav_button_back"] style:UIBarButtonItemStyleDone target:self action:@selector(back)];
    
    [self.tabPageController.tabBar addObserver:self forKeyPath:@"selectedItemIndex" options:NSKeyValueObservingOptionNew context:nil];
    [self setupRightBarButtonItem];
}
- (void)setupRightBarButtonItem {
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"点点点"] style:UIBarButtonItemStylePlain target:self action:@selector(rightBtnClick)];
}
- (void)rightBtnClick
{
    @weakify(self)
    ACActionSheet *actionSheet = [[ACActionSheet alloc] initWithTitle:@"" cancelButtonTitle:@"取消" destructiveButtonTitle:nil otherButtonTitles:@[@"意见反馈",@"诊断工具"] actionSheetBlock:^(NSInteger buttonIndex) {
        @strongify(self)
        NSLog(@"ACActionSheet block - %ld",buttonIndex);
        if (buttonIndex == 0) {
            SuggestMessageViewController * suggestVC = [[SuggestMessageViewController alloc] init];
            [self.navigationController pushViewController:suggestVC animated:YES];
        }
        if (buttonIndex == 1) {
            LCCheckToolViewController *viewController = [[LCCheckToolViewController alloc] init];
            viewController.playUrlPath = [self.mp4DetailModel.list[0].mp4_url codeString];
            [self.navigationController pushViewController:viewController animated:YES];
        }
        
    }];
    [actionSheet show];
}

/**
 *  通过KVO监控播放器状态
 *
 *  @param keyPath 监控属性
 *  @param object  监视器
 *  @param change  状态改变
 *  @param context 上下文
 */
- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary<NSString *,id> *)change context:(void *)context {
//    __weak typeof(self) weakSelf = self;
    if ([keyPath isEqualToString:@"selectedItemIndex"]) {
        if (self.tabPageController.tabBar.selectedItemIndex == 2) {
            self.lowView.hidden = NO;
        }else{
            self.lowView.hidden = YES;
            [self.view endEditing:YES];
        }
        NSLog(@"self.tabPageController.tabBar.selectedItemIndex = %zd", self.tabPageController.tabBar.selectedItemIndex);
    }
}
- (void)back{
    [self.navigationController popViewControllerAnimated:YES];
}

- (void)initViewControllers{
    NSString *id_id = self.listAboutModel.id;
    NSMutableArray *vcArray = [NSMutableArray array];
    for (NSInteger i = 0; i < 4; i++) {
        if (i == 0) {
            LCVideoAboutViewController *videoAboutViewController = [[LCVideoAboutViewController alloc] init];
            _videoAboutViewController = videoAboutViewController;
            videoAboutViewController.id_id = id_id;
            videoAboutViewController.yp_tabItemTitle = @"简介";
            [vcArray addObject:videoAboutViewController];
        }
        if (i == 1) {
            LCVideoDetailsListsViewController *videoDetailsListsViewController =
            [[LCVideoDetailsListsViewController alloc] init];
            _videoDetailsListsViewController = videoDetailsListsViewController;
            videoDetailsListsViewController.id_id = id_id;
            videoDetailsListsViewController.yp_tabItemTitle = @"节目";
            [vcArray addObject:videoDetailsListsViewController];
        }
        if (i == 2) {
            LCVideoPingLunTableViewController *videoPingLunTableViewController = [[LCVideoPingLunTableViewController alloc] init];
            _videoPingLunTableViewController = videoPingLunTableViewController;
            videoPingLunTableViewController.id_id = id_id;
            videoPingLunTableViewController.yp_tabItemTitle = @"评论";
            @weakify(self)
            [videoPingLunTableViewController.view tapGesture:^(UIGestureRecognizer *ges) {
                @strongify(self)
                [self.view endEditing:YES];
            }];
            [vcArray addObject:videoPingLunTableViewController];
        }
        if (i == 3) {
            LCVideoWithListsViewController *videoWithListsViewController = [[LCVideoWithListsViewController alloc] init];
            videoWithListsViewController.yp_tabItemTitle = @"相关";
            videoWithListsViewController.id_id = id_id;
            [vcArray addObject:videoWithListsViewController];
        }
    }
    self.tabPageController.viewControllers = vcArray;
}

- (UIView *)headerView {
    return self.titleHeaderView;
}
- (LTHeaderView *)titleHeaderView
{
    if (!_titleHeaderView) {
        _titleHeaderView = [[LTHeaderView alloc] initWithFrame:CGRectMake(0, 0, self.view.bounds.size.width, 260.0)];
        _titleHeaderView.listAboutModel = self.listAboutModel;
        @weakify(self)
        _titleHeaderView.clickShareBlock = ^{
            @strongify(self)
            [self clickShareVedio];
        };
        _titleHeaderView.clickPushLoginBlock = ^{
            @strongify(self)
            pushAccLoginVC
        };
        [_titleHeaderView.videoInfoLabel tapGesture:^(UIGestureRecognizer *ges) {
            @strongify(self)
            self.tabPageController.tabBar.selectedItemIndex = 0;
        }];
    }
    return _titleHeaderView;
}

#pragma mark - 在线视频播放的请求
- (void)onlineVideoData {
    
    @weakify(self)
    [RecommendNetWork loadLCVideoDetailChangeHeaderControllerWithVideoId:self.listAboutModel.id success:^(LCMp4DetailModel *mp4DetailModel) {
        @strongify(self)
        self.mp4DetailModel = mp4DetailModel;
        self.titleHeaderView.mp4DetailModel = mp4DetailModel;
        self.videoAboutViewController.mp4DetailModel = mp4DetailModel;
        self.videoDetailsListsViewController.mp4DetailModel = mp4DetailModel;
        self.tabPageController.tabBar.selectedItemIndex = 1;
//        }else{
//            [weakSelf setStringWarning:str];
//        }
    } failure:^(NSError *error) {
        NSLog(@"error = %@", error);
    }];
}

- (void)clickShareVedio {
    [YYKeyboardManager defaultManager].keyboardVisible ? [[[UIApplication sharedApplication] keyWindow] endEditing:YES] : -1;
    NSLog(@"clickShareVedio");
    [self shareShengJingMessageWithImageArray:@[self.mp4DetailModel.detail.pic] url:self.mp4DetailModel.list[0].mp4_url title:self.mp4DetailModel.list[0].name contentMessage:self.mp4DetailModel.detail.author type:(SSDKContentTypeWebPage)];
}

//-(LTLayout *)layout {
//    if (!_layout) {
//        _layout = [[LTLayout alloc] init];
//        _layout.pageBottomLineColor = RGBA(230, 230, 230, 1);
//        _layout.titleColor = RGBColor(140, 140, 140);
//        _layout.isAverage = YES;
//        _layout.isNeedScale = NO;
//    }
//    return _layout;
//}

- (void)setupSideslip {
    /**增加侧滑返回,无其他作用*/
    _sideslippingView = [[UIView alloc] init];
    [self.view addSubview:_sideslippingView];
    _sideslippingView.sd_layout
    .leftEqualToView(self.view)
    .topEqualToView(self.view)
    .bottomEqualToView(self.view)
    .widthIs(20);
}

- (void)viewDidLayoutSubviews {
    [super viewDidLayoutSubviews];
    
    //[self.view bringSubviewToFront:_lowView];
    [self.view bringSubviewToFront:_sideslippingView];
}

- (LCVideoHomeListAboutModel *)listAboutModel
{
    if (_listAboutModel == nil) {
        _listAboutModel = [[LCVideoHomeListAboutModel alloc] init];
    }
    return _listAboutModel;
}

- (void)createPingLun{
    @weakify(self)
    [self initWithAppraise:self.view share:1 :^(UIView *lowView,UITextView *textView,UILabel *textbguilabel, UIButton *shareBut) {
        @strongify(self)
        self.lowView = lowView;
        self.textView = textView;
        self.textbguilabel = textbguilabel;
    }];
}
@end
